package com.cxx.service.ServiceImpl;

import com.cxx.mapper.MessageMapper;
import com.cxx.mapper.UserMapper;
import com.cxx.pojo.*;
import com.cxx.service.MessageService;
import com.cxx.vo.CommentInfoVo;
import com.cxx.vo.ReplyInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<CommentInfoVo> getPersonalMessage(String account) {
        List<ReadComment> personalComments = messageMapper.getPersonalComments(account);
        List<ReadReplay> personalReplay = messageMapper.getPersonalReplay(account);
        //封装的vo类
        List<CommentInfoVo> commentInfoVoList=new ArrayList<>();
        if(personalComments.size()!=0){
        for (ReadComment personalComment : personalComments) {
            List<ReplyInfoVo> replyInfoVoList=new ArrayList<>();
            String commentAccount=personalComment.getCommentAccount();
            Comments comment=messageMapper.getCommentById(personalComment.getCommentsId());
            for (ReadReplay readReplay : personalReplay) {
                    Replay replay=messageMapper.getReplayById(readReplay.getReplyId());
                    if(userMapper.selectByAdminAccount(replay.getAccount())!=null){
                        Admin replyAdmin=userMapper.selectByAdminAccount(replay.getAccount());
                        replyInfoVoList.add(new ReplyInfoVo(replay.getAccount(),replyAdmin.getAdminImgurl(),replyAdmin.getAdminCall(),replay.getReplyTime(),replay.getReplyContent(),replay.getToAccount()));

                    }
                    else {
                        User replyUser=userMapper.selectByUserAccount(replay.getAccount());
                        replyInfoVoList.add(new ReplyInfoVo(replay.getAccount(),replyUser.getUserImgUrl(),replyUser.getUserCall(),replay.getReplyTime(),replay.getReplyContent(),replay.getToAccount()));
                    }
            }
           if(userMapper.selectByAdminAccount(commentAccount)!=null){
               Admin commentAdmin=userMapper.selectByAdminAccount(commentAccount);
               commentInfoVoList.add(new CommentInfoVo(comment.getId(),comment.getAccount(),commentAdmin.getAdminImgurl(),commentAdmin.getAdminCall(),comment.getCommentsTime(),comment.getCommentsContent(),replyInfoVoList));
           }
           else {
               User commentUser=userMapper.selectByUserAccount(commentAccount);
               commentInfoVoList.add(new CommentInfoVo(comment.getId(),comment.getAccount(),commentUser.getUserImgUrl(),commentUser.getUserCall(),comment.getCommentsTime(),comment.getCommentsContent(),replyInfoVoList));
           }
        }
        }
        else if(personalReplay.size()!=0){
            List<ReplyInfoVo> replyInfoVoList=new ArrayList<>();
            for (ReadReplay readReplay : personalReplay) {
                Replay replay=messageMapper.getReplayById(readReplay.getReplyId());
                if(userMapper.selectByAdminAccount(replay.getAccount())!=null){
                    Admin replyAdmin=userMapper.selectByAdminAccount(replay.getAccount());
                    replyInfoVoList.add(new ReplyInfoVo(replay.getAccount(),replyAdmin.getAdminImgurl(),replyAdmin.getAdminCall(),replay.getReplyTime(),replay.getReplyContent(),replay.getToAccount()));

                }
                else {
                    User replyUser=userMapper.selectByUserAccount(replay.getAccount());
                    replyInfoVoList.add(new ReplyInfoVo(replay.getAccount(),replyUser.getUserImgUrl(),replyUser.getUserCall(),replay.getReplyTime(),replay.getReplyContent(),replay.getToAccount()));
                }
            }
            commentInfoVoList.add(new CommentInfoVo(0,"0","0","0","0","0",replyInfoVoList));
        }
        return commentInfoVoList;
    }
}
